package com.ruoyi.quartz.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.quartz.service.IOrderService;
import com.ruoyi.system.domain.DTO.SocDto;
import com.ruoyi.system.domain.vo.SocVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * 部门信息
 */
@Controller
@RequestMapping("/business/order3")
public class Order3Controller extends BaseController {
    private String prefix = "business/order3";

    @Autowired
    private IOrderService orderService;

    @RequiresPermissions("business:order:view")
    @GetMapping()
    public String dept() {
        return prefix + "/dept";
    }

    @RequiresPermissions("business:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SocDto socDto) {

        List<SocVo> socVoList = new ArrayList<SocVo>();
        try {
            startPage();
            socVoList = orderService.selectList(socDto);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return getDataTable(socVoList);
    }

    @PostMapping(value = "/uploadFile/{type}")
    @ResponseBody
    public AjaxResult uploadFile(@PathVariable("type") String type,@RequestParam("file_data") MultipartFile file) throws IOException {
        if(type.equals("") || type.trim().length() == 0){
            return AjaxResult.error("缺少文件上传类型");
        }
        if (file.isEmpty()){
            return AjaxResult.error("文件为空");
        }
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String filePath = "";
        if("1".equals(type)){
            filePath = "F://jizhuangxiang//";
            File folder = new File(filePath);
            if (!folder.exists() && !folder.isDirectory()) {
                boolean result =  folder.mkdirs();
                if(!result){
                    filePath = "D://jizhuangxiang//";
                }
            }
        }else{
            filePath = "F://baoguandan//";
            File folder = new File(filePath);
            if (!folder.exists() && !folder.isDirectory()) {
                boolean result =  folder.mkdirs();
                if(!result){
                    filePath = "D://baoguandan//";
                }
            }
        }
        File dest = new File(filePath+fileName);
        if (!dest.getParentFile().exists()){
            dest.getParentFile().mkdirs();
        }
        try {

            file.transferTo(dest);
            return AjaxResult.success("成功",fileName);
        }catch(Exception e){
            e.printStackTrace();
        }

        return AjaxResult.error("失败");
    }


    /**
     * 新增
     */
    @GetMapping("/add/job")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存
     */
    @Log(title = "新增保存", businessType = BusinessType.INSERT)
    @RequiresPermissions("business:order:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@Validated SocVo socVo) throws TaskException, SchedulerException, ParseException {
        try {
            socVo.setCreateBy(ShiroUtils.getLoginName());
            return orderService.saveSocVo(socVo);
        } catch (Exception e) {
            e.getMessage();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 下单跳页
     */
    @GetMapping("/edit/{id}")
    public String add(@PathVariable("id") String id, ModelMap mmap) {
        if (id.trim().length() > 0) {
            SocVo socVo = new SocVo();
            socVo = orderService.selectById(id);
            mmap.put("socVo", socVo);
            return prefix + "/edit";
        } else {
            return "error/404";
        }
    }

    /**
     * 编辑保存
     */
    @Log(title = "编辑保存", businessType = BusinessType.INSERT)
    @RequiresPermissions("business:order:add")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@Validated SocVo socVo) throws TaskException, SchedulerException, ParseException {
        try {
            socVo.setCreateBy(ShiroUtils.getLoginName());
            return orderService.editSocVo(socVo);
        } catch (Exception e) {
            e.getMessage();
            return AjaxResult.error(e.getMessage());
        }
    }


    @Log(title = "删除", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(orderService.deleteById(ids));
    }

    @Log(title = "核销", businessType = BusinessType.DELETE)
    @PostMapping("/cancle")
    @ResponseBody
    public AjaxResult cancle(String ids)
    {
        return toAjax(orderService.cancleById(ids));
    }
    @Log(title = "复制", businessType = BusinessType.INSERT)
    @PostMapping("/copy")
    @ResponseBody
    public AjaxResult copy(String ids)
    {
        return toAjax(orderService.copy(ids));
    }
}
